package com.cby.android.library.remote

import android.annotation.SuppressLint
import android.util.Log
import okhttp3.Interceptor
import okhttp3.Response
import java.text.SimpleDateFormat
import java.util.*

/**
 * 网络请求的日志输出-拦截器
 *
 * @author chenbaoyang
 */
class LogInterceptor : Interceptor {

    private val tag = "retrofit"

    @SuppressLint("SimpleDateFormat")
    private val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")

    override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()

        //输出请求信息
        Log.i(
            tag,
            format.format(Date()) + " Requeste " + "\nmethod: ${request.method()} \nurl: ${request.url()}  \nbody:${request.body()}"
        )

        val response = chain.proceed(request)

        //响应信息
        Log.i(
            tag,
            format.format(Date()) + " Response " + "\nsuccessful: ${response.isSuccessful} \nbody: ${response.peekBody(
                1024
            ).string()}"
        )

        return response
    }
}